#
#
#
cmake_minimum_required(VERSION 2.8)

project(xlisp)
enable_testing()
set_property(GLOBAL PROPERTY USE_FOLDERS ON)

if(WIN32)
	option(XLISP_BUILD_XLISPW "Build Windows GUI" OFF)
endif()
option(XLISP_BUILD_TK "Build Unix GUI (Tk)" OFF)
option(XLISP_BUILD_QT "Build xlispqt" OFF)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

include_directories(include)

add_definitions(-DCMAKE_SIZEOF_VOID_P=${CMAKE_SIZEOF_VOID_P})

if(UNIX)
	set(CMAKE_C_FLAGS "${CMAKE_FLAGS} -Wall")
endif()

# add_subdirectory(src)

if(WIN32)
	file(GLOB SRC_SRC src/msstuff.c src/x*.c)
else()
	add_definitions(-DLOADLIB)
	file(GLOB SRC_SRC src/unstuff.c src/x*.c)
endif()
file(GLOB SRC_HDR src/x*.h include/*.h)
add_library(xlispcore SHARED ${SRC_SRC} ${SRC_HDR})
SET_PROPERTY(TARGET xlispcore PROPERTY FOLDER "lib")

if(XLISP_BUILD_TK)
	find_package(TCL)
	message(STATUS "TK_LIBRARY_VERSION = ${TK_LIBRARY_VERSION}")
	if(NOT TK_INCLUDE_PATH)
		set(TCLTK_POSSIBLE_INCLUDE_PATHS
			"/usr/local/include/tk8.1"
			"/usr/local/include/tk8.2"
			"/usr/local/include/tk8.3"
			"/usr/local/include/tk8.4"
		)
		FIND_PATH(TK_INCLUDE_PATH
			NAMES tk.h
			HINTS ${TCLTK_POSSIBLE_INCLUDE_PATHS}
		)
	endif()
	if(NOT TCL_INCLUDE_PATH)
		set(TCLTK_POSSIBLE_INCLUDE_PATHS
			"/usr/local/include/tcl8.1"
			"/usr/local/include/tcl8.2"
			"/usr/local/include/tcl8.3"
			"/usr/local/include/tcl8.4"
		)
		FIND_PATH(TCL_INCLUDE_PATH
			NAMES tcl.h
			HINTS ${TCLTK_POSSIBLE_INCLUDE_PATHS}
		)
	endif()
	message(STATUS "TK_INCLUDE_PATH = ${TK_INCLUDE_PATH}")
	message(STATUS "TCL_INCLUDE_PATH = ${TCL_INCLUDE_PATH}")
	message(STATUS "TK_LIBRARY = ${TK_LIBRARY}")
	message(STATUS "TCL_LIBRARY = ${TCL_LIBRARY}")
	# add_subdirectory(xlisptk)
	file(GLOB XLISPTK_SRC xlisptk/tkconsole.c xlisptk/tkstuff.c xlisptk/tkstuff.h xlisptk/xlisptk.c)
	file(GLOB XLISPTK_HDR xlisptk/*.h)
	include_directories(${TK_INCLUDE_PATH} ${TCL_INCLUDE_PATH} /usr/local/include)
	add_executable(xlisptk ${XLISPTK_SRC} ${XLISPTK_HDR})
	 target_link_libraries(xlisptk xlispcore ${TK_LIBRARY} ${TCL_LIBRARY})
	SET_PROPERTY(TARGET xlisptk PROPERTY FOLDER "exec")
endif()

file(GLOB XLISP_LSP *.lsp)
source_group("Lisp files" .*\\.lsp)

if(XLISP_BUILD_XLISPW)
	if(WIN32)
		# add_subdirectory(xlispw)
		file(GLOB XLISPPW_SRC xlispw/*.c xlispw/*.cpp)
		file(GLOB XLISPPW_HDR xlispw/*.h)
		add_executable(xlispw WIN32 ${XLISPPW_SRC} ${XLISPPW_HDR} ${XLISP_LSP})
		target_link_libraries(xlispw xlispcore comctl32)
		SET_PROPERTY(TARGET xlispw PROPERTY FOLDER "exec")
	endif()
endif()

# add subdirectory(ext)
file(GLOB EXT_SRC ext/*.c)
file(GLOB EXT_HDR ext/*.h)
add_library(ext SHARED ${EXT_SRC} ${EXT_HDR})
target_link_libraries(ext xlispcore)
SET_PROPERTY(TARGET ext PROPERTY FOLDER "lib")

# add_subdirectory(xlisp)

file(GLOB XLISP_SRC xlisp/*.c)
file(GLOB XLISP_HDR xlisp/*.h)
add_executable(xlisp ${XLISP_SRC} ${XLISP_HDR} ${XLISP_INCLUDE_HDR} ndi-test.lsp.cmake ${XLISP_LSP})
target_link_libraries(xlisp xlispcore)
configure_file(ndi-test.lsp.cmake ndi-test.lsp @ONLY@)
if(UNIX)
	find_library(M_LIB m)
	if(M_LIB)
		target_link_libraries(xlisp ${M_LIB})
	endif()
	find_library(DL_LIB dl)
	if(DL_LIB)
		message(STATUS "DL_LIB = ${DL_LIB}")
		target_link_libraries(xlisp ${DL_LIB})
	endif()
endif()

SET_PROPERTY(TARGET xlisp PROPERTY FOLDER "exec")

# nDI
add_subdirectory(ndi)

# Qt
if(XLISP_BUILD_QT)
	add_subdirectory(xlispqt)
endif()
